package com.zhxh.nzpos.goods;

import java.util.ArrayList;

import pl.polidea.treeview.InMemoryTreeStateManager;
import pl.polidea.treeview.TreeBuilder;
import pl.polidea.treeview.TreeStateManager;
import pl.polidea.treeview.TreeViewList;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.Constants;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.sell.ItemDeleteCallback;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 系统主界面
 * 
 * @author wangzt
 * 
 */
public class GoodsCategoryActivity extends BaseActivity implements
		CateItemCallBack {

	private ArrayList<Category> categoryItems;
	CategoryDao categoryDao;
	GoodsDao goodsDao;

	protected TreeViewList _treeView;
	protected TreeStateManager<Category> _stateManager;
	protected TreeBuilder<Category> _treeBuilder;
	Category category;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_goods_category);
		categoryDao = new CategoryDao(getApplicationContext());
		goodsDao = new GoodsDao(this);
		_stateManager = new InMemoryTreeStateManager<Category>();
		_treeBuilder = new TreeBuilder<Category>(_stateManager);
		_treeView = (TreeViewList) findViewById(R.id.mainTreeView);
		_treeView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		_treeView
				.setAdapter(new CategoryListTreeAdapter(this, _stateManager, 2));
		initViews();
	}

	private void initViews() {
		initData();
		ImageView addBtn = (ImageView) findViewById(R.id.goods_add_btn);
		addBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), CateAddActivity.class);
				// startActivity(intent);
				startActivityForResult(intent, 0);

			}
		});

	}

	private void initData() {
		categoryItems = (ArrayList) categoryDao.getAll();
		for (Category category : categoryItems) {
			if (category.getParent() == null
					|| category.getParent().length() == 0) {
				_treeBuilder.sequentiallyAddNextNode(category, 0);

				for (Category categoryChild : categoryItems) {
					if (categoryChild.getParent() != null
							&& categoryChild.getParent().length() != 0
							&& categoryChild.getParent().equals(
									category.getId())) {
						// _treeBuilder.sequentiallyAddNextNode(categoryChild,
						// 1);
						_treeBuilder.addRelation(category, categoryChild);
					}
				}
			}

		}

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (resultCode) {
		case RESULT_OK:

			String type = data.getStringExtra("type");
			Bundle b = data.getExtras();
			Category cate = (Category) b.getSerializable("cate");
			if ("update".equals(type)) {
				category.setCategory(cate.getCategory());
				_stateManager.refresh();
			} else if ("add".equals(type)) {
				_treeBuilder.addRelation(category, cate);
			} else {
				_treeBuilder.sequentiallyAddNextNode(cate, 0);
			}
		}

	}

	@Override
	public void onDelete(final Category category) {

		new AlertDialog.Builder(this)
				.setTitle(R.string.goods_category_delete_dialog_title)
				.setMessage(R.string.goods_category_delete_dialog_content)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

						// Category category = categoryItems.get(position);

						int count = goodsDao.findByCategoryId(category.getId());
						if (count > 0) {
							ViewUtil.makeToast(GoodsCategoryActivity.this,
									"请先删除相关数据");
						} else {
							categoryDao.delete(category.getId());
							_stateManager.removeNodeRecursively(category);
						}

						// 确定按钮事件
						dialog.dismiss();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// 取消按钮事件
						dialog.dismiss();
					}
				}).show();
	}

	@Override
	public void onUpdate(Category category) {
		this.category = category;
		Intent intent = new Intent();
		intent.setClass(getApplicationContext(), CateAddActivity.class);

		Bundle b = new Bundle();
		b.putSerializable("cate", category);
		intent.putExtras(b);
		intent.putExtra("type", "update");
		startActivityForResult(intent, 0);
	}

	@Override
	public void onAdd(Category category) {
		this.category = category;
		Intent intent = new Intent();
		intent.setClass(getApplicationContext(), CateAddActivity.class);
		Bundle b = new Bundle();
		b.putSerializable("cate", category);
		intent.putExtras(b);
		intent.putExtra("type", "add");
		startActivityForResult(intent, 0);
	}

}
